package com.android.sms;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.android.sms.interface_.OkHttpCallBack;
import com.android.sms.manager.API_Manager;
import com.android.sms.responce.BaseResponce;
import com.android.sms.util.SPUtil;

public class SettingActivity extends AppCompatActivity {

    TextView tv_submit, tv_send, tv_callback;
    EditText et_phone, et_host, et_content;
    ImageView public_title_left_img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        et_phone = findViewById(R.id.et_phone);
        tv_submit = findViewById(R.id.tv_submit);
        et_host = findViewById(R.id.et_host);
        public_title_left_img = findViewById(R.id.public_title_left_img);
        et_content = findViewById(R.id.et_content);
        tv_send = findViewById(R.id.tv_send);
        tv_callback = findViewById(R.id.tv_callback);

        //保存数据
        tv_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                save();
            }
        });
        //返回
        public_title_left_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        //发送数据
        tv_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String content = et_content.getText().toString();
                if (TextUtils.isEmpty(content)) {
                    Toast.makeText(SettingActivity.this, "请输入内容", Toast.LENGTH_SHORT).show();
                    return;
                }
                sendContent(content);
            }
        });

        String phone = SPUtil.getStringValue(SettingActivity.this, Constents.sp_phone, "");
        String host = SPUtil.getStringValue(SettingActivity.this, Constents.sp_host, "");
        et_phone.setText(phone);
        et_phone.setSelection(phone.length());
        et_host.setText(host);
    }

    private void save() {
        String phone = et_phone.getText().toString();
        String host = et_host.getText().toString();
        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(SettingActivity.this, "请输入手机号", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(host)) {
            Toast.makeText(SettingActivity.this, "请输入服务器地址", Toast.LENGTH_SHORT).show();
            return;
        }
        SPUtil.putValue(SettingActivity.this, Constents.sp_phone, phone);
        SPUtil.putValue(SettingActivity.this, Constents.sp_host, host);
        Toast.makeText(SettingActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
        finish();
    }

    private void sendContent(String content) {
        API_Manager.sendContent(SettingActivity.this, content, new OkHttpCallBack() {
            @Override
            public void onSuccess(BaseResponce baseResponce) {
                tv_callback.setText(baseResponce.result);
            }

            @Override
            public void onFailure(BaseResponce baseResponce) {
                tv_callback.setText(baseResponce.result);
            }
        });
    }

}